interface Num {
  val: number,
  next: Num
}

// 3->7->1  2->1
// head

function addSum (num1: Num, num2: Num): Num {
  let head: Num = {
    val: null,
    next: null
  }

  let sign = false

  let cur1 = num1
  let cur2 = num2
  let cur = head
  while (cur1 || cur2) {
    if (cur1 && cur2) {
      let val: number = cur1.val + cur2.val + Number(sign)
      if (val > 9) {
        sign = true
        val = val - 10
      } else {
        sign = false
      }
      let newNode: Num = {val, next: null}
      cur.next = newNode
      cur = newNode
      cur1 = cur1.next
      cur2 = cur2.next
    } else {
      let cur1Or2 = cur1 || cur2
      while (cur1Or2) {
        let val: number = cur1Or2.val + Number(sign)
        if (val > 9) {
          sign = true
          val = val - 10
        } else {
          sign = false
        }
        cur.next = { val, next: null}
        cur = cur.next
        cur1Or2 = cur1Or2.next
      }
    } 

  }
  if (sign) {
    cur.next = {val: 1, next: null}
  }

  return head.next
}
let num1 = {
  val: 3,
  next: {
    val: 7,
    next: {
      val: 1,
      next: <any>null
    }
  }
}

let num2 = {
  val: 3,
  next: {
    val: 7,
    next: {
      val: 9,
      next: <any>null
    }
  }
}

console.dir(addSum(num1, num2), {depth: null})